<template>
  <div>
    <!-- 搜索区域 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="学号">
        <el-input v-model="formInline.user" placeholder="学号"></el-input>
      </el-form-item>
      <el-form-item label="类型">
        <el-select v-model="formInline.region" placeholder="类型">
          <el-option label="初稿" value="初稿"></el-option>
          <el-option label="终稿" value="终稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格显示区域 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="姓名" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.student_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="学号" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{
            scope.row.article_studentId
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="专业" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.student_class }}</span>
        </template>
      </el-table-column>
      <el-table-column label="论文" width="180">
        <template slot-scope="scope">
          <el-link type="primary" @click="handleEdit(scope.$index, scope.row)">
            {{ scope.row.article_name }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="类型" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.article_type }}</span>
        </template>
      </el-table-column>
      <el-table-column label="指导教师分数" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{
            scope.row.article_teacherscore
          }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="getcord(scope.row)"> 评分 </el-button>
          <el-button size="mini" @click="getback(scope.row)"> 撤回 </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 评分弹框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="评分" prop="mark">
          <el-input v-model="form.mark"></el-input>
        </el-form-item>
        <el-form-item label="评语" prop="desc">
          <el-input type="textarea" v-model="form.desc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="truecord()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  created() {
    this.get_studentpaper();
  },
  data() {
    return {
      form: {
        mark: "",
      },
      rules: {
        mark: { required: true, message: "必填", trigger: "blur" },
        desc: { required: true, message: "必填", trigger: "blur" },
      },
      rows: {},
      dialogVisible: false,
      tableData: [],
      table: [],
      formInline: {},
    };
  },
  methods: {
    onSubmit() {
      this.tableData = this.table.filter((val) => {
        return (
          (this.formInline.user == val.article_studentId &&
            this.formInline.region == val.article_type) ||
          (!this.formInline.user &&
            this.formInline.region == val.article_type) ||
          (this.formInline.user == val.article_studentId &&
            !this.formInline.region) ||
          (!this.formInline.user && !this.formInline.region)
        );
      });
      // console.log(this.tableData);
    },
    // 下载
    handleEdit(index, row) {
      location.href = "http://127.0.0.1:8080/api/teacher_download?id=" + row.id;
    },
    // 获取学生论文1信息
    get_studentpaper() {
      this.$http
        .get("http://127.0.0.1:8080/teacher/student_paper")
        .then((res) => {
          // console.log(res);
          if (res.data.status == 0) {
            this.tableData = res.data.data;
            this.table = res.data.data;
          } else {
            this.tableData = [];
          }
          // console.log(this.tableData.student_name);
        });
    },
    getcord(row) {
      if (row.article_type == "初稿") {
        this.$message.error("初稿不能打分");
        this.dialogVisible = false;
      } else {
        this.dialogVisible = true;
        this.rows = row;
      }
    },

    truecord() {
      // console.log(this.form);
      this.$refs.form.validate((valid) => {
        if (!valid) return;
        this.$http
          .post(
            "http://127.0.0.1:8080/teacher/teacher_mark",
            this.$qs.stringify({
              ...this.rows,
              ...this.form,
            })
          )
          .then((res) => {
            // console.log(res);
            if (res.data.status == 0) {
              this.get_studentpaper();
              this.$message.success(res.data.message);
              this.dialogVisible = false;
              this.form = {};
            } else {
              this.$message.error(res.data.message);
            }
          })
          .catch((err) => {
            this.$message.error(err);
          });
      });
    },
    getback(row) {
      this.$http
        .post("http://127.0.0.1:8080/teacher/noPass", this.$qs.stringify(row))
        .then((res) => {
          if (res.data.status == 0) {
            this.$message.success(res.data.message);
            this.get_studentpaper();
          } else {
            this.$message.error(res.data.message);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
      // console.log(row);
    },
  },
};
</script>
<style lang="less" scoped>
</style>
